package com.wcl.demo.ui.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.wcl.demo.R;
import com.wcl.demo.model.entity.Mp3Info;
import com.wcl.demo.view.FormatHelper;
import com.wcl.demo.view.MusicLoader;

import java.util.List;
import com.wcl.demo.view.MusicLoader.MusicInfo;

/**
 * Created by dllo on 15/11/21.
 */
public class MusicAdapter extends BaseAdapter {
    private List<MusicInfo> datas;

    public void addData(List<MusicInfo> datas) {
        this.datas = datas;
    }

    @Override
    public int getCount() {
        return datas != null && datas.size() > 0 ? datas.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return datas != null && datas.size() > 0 ? datas.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return datas.get(position).getId();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder=null;
        if(convertView == null){
            holder= new ViewHolder();
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.music_item, null);
            holder.imageView = (ImageView) convertView.findViewById(R.id.albumPhoto);
            holder.title = (TextView) convertView.findViewById(R.id.title);
            holder.duration = (TextView) convertView.findViewById(R.id.duration);
            holder. artist = (TextView) convertView.findViewById(R.id.artist);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }

        holder.imageView.setImageResource(R.mipmap.ic_launcher);
        holder.title.setText(datas.get(position).getTitle());
        holder.duration.setText(FormatHelper.formatDuration(Integer.parseInt(datas.get(position).getDuration() + "")));
        holder.artist.setText(datas.get(position).getArtist());

        return convertView;
    }

    class ViewHolder {
        private ImageView imageView;
        private TextView title;
        private TextView duration;
        private TextView artist;

    }
}
